import pandas as pd
from openpyxl import load_workbook


# 一维数组
s = pd.Series([9, 'zheng', 'beijing', 128])
# print(s)

data = {"state": [1, 24, 56, 100], "year": [2008, 2012, 2021, 2022], "name": ["li", "wang", "chen", "zhang"]}

# 二维的表格型数据结构
# df = pd.DataFrame(data)
# print(df["year"])  # 输出字典key为“year”的value值
# print(type(df["year"]))  # 输出<class 'pandas.core.series.Series'>
# print(df.loc[0])  # 输出第一列数据


# 加载现有的 Excel 文件
book = load_workbook('output.xlsx')

# 创建 ExcelWriter 对象
excel_writer = pd.ExcelWriter('output.xlsx', engine='openpyxl')
excel_writer.book = book  # 读取已有的excel文件

# 创建 DataFrame
df2 = pd.DataFrame(data)

# 将 DataFrame 写入指定的工作表
df2.to_excel(excel_writer, sheet_name='helloworld')

# 保存文件
excel_writer.save()

# 关闭 ExcelWriter 对象
excel_writer.close()